package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * @name http://www.rc929.com/
 * @link 929 人才网
 * @author dhy
 */ 
public class Rc929NewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://[a-z]*.rc929.com/job/job.asp\\?JobId=[0-9]*",
		"http://www.rc929.com/News/New.asp\\?NewsId=[0-9]*",
		"http://[a-z]*.rc929.com/Persons/Person.asp\\?PerId=[0-9]*",
		"http://zouping.rc929.com/zhaopin/qiye.asp\\?ComId=[0-9]*"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		
		Elements titleList = doc.select("div.jycomv4222");
		if(titleList.size()>0)
			title = titleList.get(0).text();
		if(StringUtils.isBlank(title))
			title = doc.select("div.jobsnews111"	).text();
		if(StringUtils.isBlank(title))
			title = doc.select("li.jobsnews111").text();
		if(StringUtils.isBlank(title))
			title = doc.select("td[height=30]").text();
		
		Elements dateList = doc.select("div.jycomv4227 li");
		if(dateList.size() > 9)
			date = DateUtils.matchDate(dateList.get(9).text());
		if(date==null)
			date = DateUtils.matchDate(doc.select("div.jobsnews112").text());
		if(date==null)
			date = DateUtils.matchDate(doc.select("li.jobsnews112").text());
		if(date==null)
			date = DateUtils.matchDate(doc.select("td[height=20]").text());
		if(date==null)
		{
			dateList = doc.select("div.jycomv413 li");
			if(dateList.size() >= 8)
				date = DateUtils.matchDate(dateList.get(7).text());
		}
		
		content =HtmlCleaner.getContentHtml(url, doc.select("div.jycomv4227"));
		if(StringUtils.isBlank(content))
			content =HtmlCleaner.getContentHtml(url, doc.select("div.jobsnews12"));
		if(StringUtils.isBlank(content))
			content =HtmlCleaner.getContentHtml(url, doc.select("table.border1"));
		if(StringUtils.isBlank(content))
			content =HtmlCleaner.getContentHtml(url, doc.select("div.jycomv4223"));
		
		author=JSoupUtils.matchAuthor(doc, "来源：");
		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);

		return news;

	}

	public static void main(String[] args) {
		Rc929NewsAnalyse analyse=new Rc929NewsAnalyse();
		String url="http://zouping.rc929.com/zhaopin/qiye.asp?ComId=24658";
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(UrlArgumentTop.FromatUrl(url));
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}

	
